import { defineStore } from "pinia";

import { useNProgress } from '@vueuse/integrations/useNProgress'
const { isLoading } = useNProgress()
import 'nprogress/nprogress.css'
import {require} from "@/modules/axios"
export const useStore=defineStore("main",{
	state:()=>({
		isLoading,
		test:11,
		token:"",
		username:"",
		isLogin:false,
		data1:[]
	}),
	getters:{
		data:async (state)=>{
			console.log("this.data:",state.data1);
			if(state.data1.length!==0){
				return state.data1
			}else{
				return await require.get("../data.json").then(res=>{
					state.data1=res.data
					return res.data
				})
			}
		}
	},
	actions:{
		login({username,token}){
			this.isLogin=true
			this.username=username
			this.token=token
			console.log("this:",this);
		},
		exitLogin(){
			this.isLogin=false
			this.username=""
			this.token=""
		},
		isLoadingStart(){
			this.isLoading=true
		},
		isLoadingEnd(){
			this.isLoading=false
		},
		addData(data){
			this.data1=data
		}
	},
    persist: {
      key: 'storekey', //缓存key
      storage: window.sessionStorage, //缓存方式
      // 部分持久化状态的点符号路径数组，默认持久化所有数据 
      paths: ['username',"isLogin","token"],  //持久化counter 字段
    }
})